/* 查询所有用户的网络请求 */
export const userListApi=()=>{
    return new Promise((resolve,reject)=>{
        const xhr=new XMLHttpRequest()
        xhr.open('GET','http://127.0.0.1:3002/users/userList',true)
        xhr.setRequestHeader('Authorization',localStorage.getItem('token'))
        xhr.send()
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4){
                if(xhr.status==200){
                    resolve(JSON.parse(xhr.responseText))
                }else if(xhr.status==401){
                    reject('401err')
                }
            }
        }
    })
}
/* 通过_id删除用户的api */
export const deleteUserApi=(_id)=>{
    return new Promise((resolve,reject)=>{
        const xhr=new XMLHttpRequest()
        xhr.open('DELETE',`http://127.0.0.1:3002/users/delUser/${_id}`,true)
        xhr.setRequestHeader('Authorization',localStorage.getItem('token'))
        xhr.send()
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4){
                if(xhr.status==200){
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const loginApi=user=>{
    return new Promise((resolve,reject)=>{
        const xhr=new XMLHttpRequest()
        xhr.open('POST',"http://127.0.0.1:3002/users/login",true)
        xhr.setRequestHeader('Content-Type',"application/json")
        xhr.send(JSON.stringify(user))
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4){
                if(xhr.status==200){
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}